using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Security.Authentication;
using System.Web.Security;
using System.Web.UI.WebControls;
using iPhOwa.Exchange;
using iPhOwa.Web.Properties;

namespace iPhOwa.Web
{
    /// <summary>
    /// Form for site login.
    /// </summary>
    [SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Login")]
    public partial class Login : iPhOwaPage
    {
        /// <summary>
        /// Determines if we need to auth for this page.
        /// </summary>
        protected override bool IsSecurePage
        {
            get { return false; }
        }

        /// <summary>
        /// Page_Load Event
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string username = GetCookie(UserNameCookie);
                string password = GetCookie(PasswordCookie);

                if (!string.IsNullOrEmpty(username))
                {
                    loginControl.UserName = username;
                    loginControl.RememberMeSet = true;

                    if (!string.IsNullOrEmpty(password))
                    {
                        try
                        {
                            Authenticate(username, password);

                            FormsAuthentication.RedirectFromLoginPage(username, false);
                            Response.End();
                        }
                        catch (AuthenticationException)
                        {
                        }
                    }
                }
            }
        }

        /// <summary>
        /// FormsAuthenticate event.
        /// </summary>
        [SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Login"),
         SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
        protected void LoginControl_Authenticate(object sender, AuthenticateEventArgs e)
        {
            try
            {
                Authenticate(loginControl.UserName, loginControl.Password);

                e.Authenticated = true;
            }
            catch (AuthenticationException)
            {
                e.Authenticated = false;
            }
        }

        /// <summary>
        /// Call OWA for authentication.
        /// </summary>
        [SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters",
            MessageId = "System.NotSupportedException.#ctor(System.String)")]
        private void Authenticate(string username, string password)
        {
            if (AuthenticationType == AuthenticationType.Forms)
            {
                CookieContainer cookies = OwaAuthenticator.FormsAuthenticate(Settings.Default.OwaUrl, username, password);

                OwaCookies = cookies;
                UserName = username;
            }
            else
            {
                UserName = username;
                Password = password;
            }

            if (loginControl.RememberMeSet)
            {
                SetCookie(UserNameCookie, username);
                SetCookie(PasswordCookie, password);
            }
            else
            {
                ExpireCookie(UserNameCookie);
                ExpireCookie(PasswordCookie);
            }
        }
    }
}